//
//  MethodViewController.m
//  FoodFamily
//
//  Created by dlios on 15-6-20.
//  Copyright (c) 2015年 guoBang. All rights reserved.
//

#import "MethodViewController.h"

@interface MethodViewController ()

@end

@implementation MethodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
#pragma beijing
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 1000)];
    imageview.image = [UIImage imageNamed:@"beijing.png"];
    [self.view addSubview:imageview];
    
    
    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationItem.title = @"做法";
    
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStylePlain target:self action:@selector(qwer)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
       flowLayout.itemSize = CGSizeMake(110, 150);//设置item 大小
    flowLayout.headerReferenceSize = CGSizeMake(WIDTH, 120);//头部区域视图大小
    flowLayout.footerReferenceSize = CGSizeMake(WIDTH, 120);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;//上下gun动
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    
    
    if (WIDTH<375) {
        
                flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
        //        NSLog(@"1111");
        //        flowLayout.minimumInteritemSpacing = 20;
        //        flowLayout.minimumLineSpacing = 20;
        flowLayout.itemSize = CGSizeMake(130, 150);//设置item 大小
        
    }

    self.collection = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:flowLayout];//负责显示的
    self.collection.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.collection];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
#pragma 注册cell
    [self.collection registerClass:[MethodCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
#pragma 注册头部区域
    [self.collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
#pragma  注册底部区域
    [self.collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    [self AfnMETHOD];
    }
//设置表头和底部区域内容

-(void)AfnMETHOD
{
    NSString *string = [NSString stringWithFormat:@"http://121.41.88.194:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=%@&type=2&phonetype=0&is_traditional=0",self.vegetable_id];
    
    
    [AFNGetDataValue getDataAFN:string block:^(NSMutableDictionary *dataDic) {
    NSMutableArray *ARRAY = [NSMutableArray array];
    ARRAY = [dataDic objectForKey:@"data"];
    self.methdoArray = [NSMutableArray array];
    for (NSMutableDictionary *dic in ARRAY) {
        Movie *movie = [[Movie alloc]init];
        movie.order_id = [dic objectForKey:@"order_id"];
        movie.describe = [dic objectForKey:@"describe"];
        movie.imagePath = [dic objectForKey:@"imagePath"];
        [self.methdoArray addObject:movie];
        [movie release];
    
    }
    
    [self.collection reloadData];
}];


}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{//kind是显示区域类型
    if (kind ==UICollectionElementKindSectionHeader) {
        MethodCollectionViewCell *headerview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        headerview.backgroundColor = [UIColor purpleColor];
        
        MethodView *method = [[MethodView alloc]init];
        method.mov = self.movie;
        [headerview addSubview:method];

       
        
       //NSLog(@"***********%@",method.mov.name);
        return headerview;
    }else
    {
        MethodCollectionViewCell *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        return footerview;
    
    }

}
#pragma 点击某个item
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detail = [[DetailViewController alloc]init];
    detail.movie = self.movie;
    detail.dearray = [NSMutableArray arrayWithArray:self.methdoArray];
       //[self.navigationController pushViewController:detail animated:YES];
   }
//设置分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{


    return 1;
}
#pragma 设置item的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    
    return self.methdoArray.count;
}
#pragma 布局item
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
// 直接从系统的重用池里取cell
    MethodCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
   
    Movie *movie = [self.methdoArray objectAtIndex:indexPath.row];
    cell.movie  = movie;
    //NSLog(@"cell.movie ===%@",cell.movie);
    return cell;
}
-(void)qwer
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
